from subprocess import call
import datetime
import os


def runAPDL(ansyscall,numprocessors,workingdir,scriptFilename):
    """
    runs the APDL script: input_IA2.inp
    located in the folder: workingdir
    using APDL executable invoked by: ansyscall
    using the number of processors in: numprocessors
    returns the number of Ansys errors encountered in the run
    """
    inputFile = os.path.join(C:\\Users\Miranda\Desktop\Testing_Runs,
                             input_IA2+".inp")
    # make the output file be the input file plus timestamp
    outputFile = os.path.join(C:\\Users\Miranda\Desktop\Testing_Runs,
                              input_IA2+
                              '{:%Y%m%d%H%M%S}'.format(datetime.datetime.now())+
                              ".out")
    # keep the standard ansys jobname
    jobname = "Ventricle"
    callString = ("\"{}\" -p ansys -dis -mpi INTELMPI"
              " -np {} -dir \"{}\" -j \"{}\" -s read"
              " -b -i \"{}\" -o \"{}\"").format(
                      ansyscall,
                      numprocessors,
                      workingdir,
                      jobname,
                      inputFile,
                      outputFile)
    print("invoking ansys with: ",callString)
    call(callString,shell=False)

    # check output file for errors
    print("checking for errors")
    numerrors = "undetermined"
    try:
        searchfile = open(input_IA2+'{:%Y%m%d%H%M%S}'.format(datetime.datetime.now())+".out", "r")
    except:
        print("could not open",input_IA2+'{:%Y%m%d%H%M%S}'.format(datetime.datetime.now())+".out")
    else:
        for line in searchfile:
            if "NUMBER OF ERROR" in line:
                print(line)
                numerrors = int(line.split()[-1])
        searchfile.close()
    return(numerrors)


def main():
    ansyscall = "C:\Program Files\ANSYS Inc\v172\ansys\bin\winx64\\MAPDL.exe"
    numprocessors = 3
    workingdir = "C:\Users\Miranda\Desktop\Testing_Runs"
    scriptFilename = "input_IA2"
    nErr = runAPDL(ansyscall,
                   numprocessors,
                   workingdir,
                   scriptFilename)
    print ("number of Ansys errors: ",nErr)


if __name__ == '__main__':
     main()
